package org.example.demo2.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.demo2.common.PageData;
import org.example.demo2.common.ResponseData;
import org.example.demo2.mapper.UserMapper;
import org.example.demo2.pojo.StuInfo;
import org.example.demo2.pojo.UserInfo;
import org.example.demo2.pojo.UserLogin;
import org.example.demo2.pojo.UserQuery;
import org.example.demo2.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    String symbol = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()\\-_=+{};:,<.>\\/?]).{8,}$";

    @Override
    public ResponseData<UserInfo> signUser(UserInfo userInfo){

        if(StrUtil.isBlank(userInfo.getUserName())){
            return ResponseData.fail("用户名不能为空");
        }
        if(StrUtil.isBlank(userInfo.getPwd())){
            return ResponseData.fail("密码不能为空");
        }
        if(StrUtil.isBlank(userInfo.getAge())){
            return ResponseData.fail("年龄不能为空");
        }
        if (userInfo.getPwd().length()<8) {
            return ResponseData.fail("密码长度必须大于8");
        }
        if (userInfo.getPwd().matches(symbol)) {
            return ResponseData.fail("密码必须包含特殊字符");
        }
        userInfo.setPwd(SecureUtil.sha256(userInfo.getPwd()));
        UserInfo getUser = userMapper.getUser(userInfo.getUserName());
        if(ObjectUtil.isNotEmpty(getUser)){
            return ResponseData.fail("用户已存在",userInfo);
        }
        int i = userMapper.insertUser(userInfo);
        if (i <= 0){
            return ResponseData.fail("请求失败");
        }
        return ResponseData.success(userInfo);
    }

    @Override
    public ResponseData<UserLogin> loginUser(UserLogin userLogin){
        if(StrUtil.isBlank(userLogin.getUserName())){
            return ResponseData.fail("用户名不能为空");
        }
        if(StrUtil.isBlank(userLogin.getPwd())){
            return ResponseData.fail("密码不能为空");
        }
        UserInfo getUser = userMapper.getUser(userLogin.getUserName());
        if(!ObjectUtil.isNotNull(getUser)){
            return ResponseData.fail("用户名不存在");
        }
        String pwd=SecureUtil.sha256(userLogin.getPwd());
        if(StrUtil.equals(pwd,getUser.getPwd())){
            return ResponseData.fail("密码错误");
        }
        return ResponseData.success("请求成功",userLogin);
    }

    @Override
    public ResponseData<UserInfo> deleteUser(String userName) {
        int i=userMapper.deleteUser(userName);
        if(i<1){
            return ResponseData.fail("删除失败");
        }
        return ResponseData.success();
    }
    @Override
    public ResponseData<PageData<UserInfo>> page(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPageStart(),userQuery.getPageSize());
        PageInfo<UserInfo> pageInfo=new PageInfo<>(userMapper.pageQuery(userQuery));
        PageData<UserInfo> pageData=new PageData<>(pageInfo.getTotal(),pageInfo.getList());
        return ResponseData.success(pageData);
    }

    @Override
    public ResponseData<StuInfo> updateStu(StuInfo stuInfo) {
        StuInfo getStu = userMapper.getStu(stuInfo.getStuId());
        if(ObjectUtil.isNotEmpty(getStu)){
            userMapper.updateStu(stuInfo);
            userMapper.addstuResumeList(stuInfo.getStuResumeList());
            return ResponseData.success("更新成功",stuInfo);
        }else {
            int i = userMapper.insertStu(stuInfo);
            userMapper.addstuResumeList(stuInfo.getStuResumeList());
            if (i <= 0){
                return ResponseData.fail("添加失败",stuInfo);
            }
            return ResponseData.success("添加成功",stuInfo);
        }
    }

    @Override
    public ResponseData<String> upload(MultipartFile file) {
        try {
            //获取文件名
            String fileName = file.getOriginalFilename();
            //保存文件到本地
            file.transferTo(new File("D:/test"  + fileName));
            return ResponseData.success();
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseData.fail("上传失败");
        }
    }

    @Override
    public ResponseData<UserInfo> getUser(String userName) {
        UserInfo userInfo=userMapper.getUser(userName);
        return ResponseData.success(userInfo);
    }
}
